package com.mentaleye.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

public class FileUtils {
	/**
	 * �ļ��ϴ�
	 * 
	 * @param is
	 *            ��servlet��ͨ��getServletContext()�õ��ö���
	 * @param context
	 *            ������ һ��ͨ��fileitem.getInputStream();�õ�
	 * @param path
	 *            �ϴ��������·��
	 * @param realname
	 *            �ϴ�����ļ���
	 * @return
	 */

	public static boolean upload(InputStream is, 
			String path, String realname) {
		boolean result = false;
		//String realpath = context.getRealPath(path);// �õ�һ������·��
		File file = new File(path, realname);// ��path·���´�����һ����name�Ķ���
		FileOutputStream os = null;
		try {
			file.createNewFile(); // ����һ�����ļ�
			os = new FileOutputStream(file);
			byte[] datas = new byte[1024];
			int length = 0;

			while ((length = is.read(datas)) > 0) {
				os.write(datas, 0, length);
			}

			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

		return result;

	}
	/**
	 * 
	 * @param context
	 * @param response
	 * @param path   ���Ǹ�·������(����·��)
	 * @param realname   �����Ǹ��ļ�
	 * @param filename  ���غ���ļ�������
	 * @return 0--ʧ�� 1--�ɹ� -1--�ļ�������
	 */
	
	public static int download(ServletContext context,
			HttpServletResponse response, String path, String realname,
			String filename) {
		int result = 0;

		try {
			filename = URLEncoder.encode(filename, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		FileInputStream is = null;
		OutputStream os = null;
		try {
			String realpath = context.getRealPath(path); // �õ�һ����ʵ·��
			File file = new File(realpath, realname);
			if (file.exists()) {

				is = new FileInputStream(file);
				response.reset();
				int length = is.available();// �����������ĳ���
				response.setHeader("Content-Disposition",
						"attachment;filename=" + filename);// attachment
															// ָ�����Ը�������ʽ
				response.setContentLength(length);// ����������ĳ���

				os = response.getOutputStream();
				byte[] datas = new byte[1024];
				int len = 0;
				int i=0;
				while ((len = is.read(datas)) > 0) {
					os.write(datas, 0, len);
					i++;
					if(i%3==0){
						os.flush();
					}
				}
				result=1;
			}else{
				result=-1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return result;
	}

    public static String getNewFileName(String oldFileName){
    	long nowTime = new Date().getTime();
    	int suffixIndex=oldFileName.lastIndexOf(".")+1;
    	return String.valueOf(nowTime)+"."+oldFileName.substring(suffixIndex);
    }
}
